<template>
	<view class="content_goods">
		<view class="header_top">
			<view class="search_card">
				<view class="left_s">
					<uni-easyinput
						prefixIcon="search"
						@keydown.enter="OrderGoodsList"
						placeholderStyle="font-size:28rpx;"
						v-model="keyword"
						:inputBorder="false"
						placeholder="输入型号/名称/规格搜索"
						@clear="orderGoodsList"
						@confirm="OrderGoodsList"
						@input="OrderGoodsList"
					></uni-easyinput>
					<view class="search_btn" @click="OrderGoodsList">搜索</view>
				</view>
			</view>
		</view>
		<view class="bodyer">
			<view class="goods_list" v-if="orderGoodsList.length > 0">
				<view
					class="goods_item"
					v-for="(item, index) in orderGoodsList"
					:key="index"
				>
					<view class="goods_item_left">
						<view class="item_img">
							<image
								:src="item.image"
								v-if="item.image"
								@click="lookImg(item.image)"
								class="_img"
								mode="aspectFill"
							></image>
							<uni-icons color="#aac" size="48" v-else type="image"></uni-icons>
						</view>

						<view class="item_title">
							<view class="item_title_brand">
								{{ item.name }}
							</view>
							<view class="item_title_text">
								编号：
								<span class="item_title_color">{{ item.code }}</span>
							</view>
							<view class="item_title_text">
								型号：
								<span class="item_title_color">{{ item.model }}</span>
							</view>
							<view class="item_title_text">
								规格：
								<span class="item_title_color">{{ item.specName }}</span>
							</view>
						</view>
					</view>
					<uni-icons
						type="plus-filled"
						size="26"
						@click="addGoods(item)"
						color="#007cff"
						v-if="
							!seleMygoodsList.find(
								(o) => o.specId == item.specId && o.id == item.id,
							)
						"
					></uni-icons>
					<uni-icons
						type="minus-filled"
						size="26"
						@click="deleteGoods(item)"
						v-else
						color="#FF5733"
					></uni-icons>
				</view>
				<view class="no_some" v-if="title">
					{{ title }}
				</view>
			</view>
			<view
				v-if="keywordSearch && orderGoodsList.length == 0"
				class="void_goods"
			>
				<image class="img" src="/static/goods.svg" mode="aspectFit"></image>
				<view class="tip">查无商品信息</view>
			</view>
			<view v-else-if="orderGoodsList.length == 0" class="void_goods">
				<!-- <image class="img" src="/static/goods.svg" mode="aspectFit"></image> -->
				<view class="tip">
					搜索查询...
					<uni-icons type="search" color="#999"></uni-icons>
				</view>
			</view>
			<!-- 提交按钮 -->
			<view class="activeBtn" v-if="seleMygoodsList.length > 0">
				<view class="btn_Confirm">
					<view class="show_Confirm">
						已选择&nbsp;
						<span style="color: #007cff; font-weight: bold">
							{{ seleMygoodsList.length }}
						</span>
						&nbsp;个产品
						<view class="details" @click="lookDetail()">
							明细
							<uni-icons type="top" color="#007cff"></uni-icons>
						</view>
					</view>
					<view class="btn" @click="btnSubmit">
						<view class="text">确认提交</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 明细弹窗 -->
		<uni-popup
			ref="popupMyGoodsDetailRef"
			type="bottom"
			background-color="#eee"
		>
			<view class="goodspopup">
				<view class="text_top_popup">
					已选择商品
					<uni-icons
						class="icon"
						color="#333"
						@click="popupMyGoodsDetailRef.close()"
						size="21"
						type="closeempty"
					></uni-icons>
				</view>
				<view class="">
					<view class="goods_list" v-if="seleMygoodsList.length > 0">
						<view
							class="goods_item"
							v-for="(item, index) in seleMygoodsList"
							:key="index"
						>
							<view class="goods_item_left">
								<view class="item_img">
									<image
										:src="item.image"
										v-if="item.image"
										@click="lookImg(item.image)"
										class="_img"
										mode="aspectFill"
									></image>
									<uni-icons
										color="#aac"
										size="48"
										v-else
										type="image"
									></uni-icons>
								</view>
								<view class="item_title">
									<view class="item_title_brand">
										{{ item.name }}
									</view>
									<view class="item_title_text">
										<span>型号：</span>
										<span class="item_title_color">{{ item.model }}</span>
									</view>
									<view class="item_title_text">
										<span>规格：</span>
										<span class="item_title_color">{{ item.specName }}</span>
									</view>
									<view class="item_title_code" v-if="item.code">
										<span>SN码/一物一码：</span>
										<span class="item_title_color">{{ item.code }}</span>
									</view>
								</view>
							</view>
							<uni-icons
								type="minus-filled"
								size="26"
								@click="deleteGoods(item)"
								color="#FF5733"
							></uni-icons>
						</view>
					</view>
					<view v-else class="void_goods">
						<image class="img" src="/static/goods.svg" mode="aspectFit"></image>
						<view class="tip">请添加商品</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import {ref, watch} from 'vue';
	import apiGoods from '@/util/api/goods.js';
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh,
		onUnload,
	} from '@dcloudio/uni-app';
	import store from '@/store/index.js';
	import _ from 'lodash';
	// 通过onLoad接收数据
	onLoad(async (e) => {});

	onUnload(() => {
		uni.hideLoading();
	});
	const orderGoodsList = ref([]); //商品列表
	const keyword = ref(''); //搜索关键词：商品型号/商品名称/商品规格
	const popupMyGoodsDetailRef = ref(); //弹窗dom元素
	const page = ref(1);
	const total = ref(0);
	const title = ref('没有更多了');
	//显示已选择 我的商品 客户已登记商品 id列表
	const seleMygoodsList = ref([]);
	const keywordSearch = ref(false);
	// 获取列表
	const OrderGoodsList = _.debounce(
		async () => {
			if (!keyword.value.trim()) {
				orderGoodsList.value = [];
				total.value = 0;
				page.value = 1;
				return;
			}
			let data = {
				keyword: keyword.value.trim(), //搜索关键词：商品型号/商品名称/商品规格
				limit: 0,
				page: page.value,
				typeId: '',
				typeThreeId: '',
				typeTwoId: '',
			};
			uni.showLoading({
				title: '加载中...',
			});
			await apiGoods.getOrderGoodsList(data).then((res) => {
				if (res.code == 20000) {
					if (page.value == 1) {
						orderGoodsList.value = res.data.rows;
					} else {
						orderGoodsList.value = [...orderGoodsList.value, ...res.data.rows];
					}
					keywordSearch.value = orderGoodsList.value.length == 0;
					total.value = res.data.total;
				}
			});
			uni.hideLoading();
		},
		600,
		{leading: false},
	);
	// 底部加载更多
	onReachBottom(() => {
		if (orderGoodsList.value.length < total.value) {
			page.value += 1;
			OrderGoodsList();
		}
	});
	// 下拉刷新
	onPullDownRefresh(async () => {
		page.value = 1;
		keyword.value = '';
		await OrderGoodsList();
		uni.stopPullDownRefresh();
	});
	// 查看明细
	const lookDetail = () => {
		popupMyGoodsDetailRef.value.open();
	};
	// 确认提交
	const btnSubmit = () => {
		uni.navigateBack();
		let arr = seleMygoodsList.value.map((item) => {
			return {
				code: '',
				descr: '',
				goodsId: item.id,
				goodsName: item.name,
				goodsSpecId: item.specId,
				goodsSpecName: item.specName,
				paths: [],
				isShow: false,
				imgarr: [],
			};
		});
		store.commit('set_SelectVoGoods', arr);
		popupMyGoodsDetailRef.value.close();
	};
	// 预览图片
	const lookImg = (url) => {
		uni.previewImage({
			urls: [url],
		});
	};
	// 添加商品
	const addGoods = (item) => {
		let tmp = {
			codeIds: [],
			id: item.id,
			specId: item.specId,
			name: item.name,
			specName: item.specName,
			code: '',
			brandId: item.brandId,
			brand: item.brand,
			typeId: item.typeId,
			type: item.type,
			model: item.model,
			image: item.image,
		};
		seleMygoodsList.value.push(tmp);
	};
	// 删除商品
	const deleteGoods = (item) => {
		seleMygoodsList.value = seleMygoodsList.value.filter((o) => {
			if (!(o.id == item.id && o.specId == item.specId)) {
				return o;
			}
		});
	};
</script>

<style lang="scss">
	.content_goods {
		width: 100vw;
		height: 100vh;

		background-color: #fafafa;

		.header_top {
			padding: 10rpx 0;
			background-color: #f6f7fa;
			position: fixed;
			width: 100%;
			z-index: 9;

			.search_card {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 10rpx 24rpx;

				.left_s {
					display: flex;
					align-items: center;
					background-color: #fff;
					border-radius: 40rpx;
					height: 80rpx;
					width: 100%;

					:deep(.uni-easyinput__content) {
						height: 80rpx;
						border-radius: 40rpx 0 0 40rpx;

						&.is-focused {
							.uniui-clear,
							.uniui-eye-filled {
								color: #007cff99 !important;
							}
						}
					}

					.search_btn {
						font-size: 28rpx;
						border-left: 2rpx solid #007cff;
						color: #000;
						line-height: 40rpx;
						padding: 0 20rpx;
						height: 40rpx;
						color: #007cff;
						border-radius: 0 40rpx 40rpx 0;
					}
				}

				.my_goods {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					flex-wrap: nowrap;
					position: relative;

					.text_my {
						white-space: nowrap;
						color: #007cff;
					}

					.img_msg {
						width: 35rpx;
						height: 35rpx;
						margin-left: 10rpx;
						margin-right: 4rpx;
						border-radius: 50%;
					}

					.red_num {
						background-color: #ff5733;
						width: 40rpx;
						height: 40rpx;
						line-height: 40rpx;
						position: absolute;
						top: -30rpx;
						right: -20rpx;
						text-align: center;
						border-radius: 50%;
						color: #fff;
						font-size: 22rpx;
						transform: scale(0.8);
					}
				}
			}
		}

		.bodyer {
			width: 100%;
			display: flex;
			background-color: #fafafa;

			.goods_list {
				margin: 0 24rpx;
				width: 100%;
				margin-top: 100rpx;

				.no_some {
					font-size: 24rpx;
					color: #ccc;
					text-align: center;
					width: 100%;
					padding-bottom: 200rpx;
				}

				.goods_item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					background-color: #fff;
					margin: 24rpx 0;
					border-radius: 10rpx;
					padding: 24rpx;

					.goods_item_left {
						display: flex;
						align-items: center;

						.item_img {
							width: 140rpx;
							height: 140rpx;
							background-color: #fff;
							border-radius: 4rpx;
							border: 1px solid #eee;
						}

						._img {
							width: 140rpx;
							height: 140rpx;
							border-radius: 4rpx;
						}

						.item_title {
							font-size: 24rpx;
							margin-left: 24rpx;
							flex: 1;

							.item_title_name {
								margin-bottom: 10rpx;
								font-weight: bold;
								font-size: 28rpx;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
								overflow: hidden;
								word-wrap: break-word;
								word-break: break-all;
							}

							.item_title_brand {
								font-size: 28rpx;
								margin-bottom: 10rpx;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
								overflow: hidden;
								word-wrap: break-word;
								word-break: break-all;

								.dot {
									font-weight: bold;
									padding: 0 10rpx;
								}
							}

							.item_title_text {
								font-size: 24rpx;
								color: #777;
								margin-bottom: 10rpx;
								display: flex;
								flex-wrap: wrap;

								.item_title_color {
									color: #000;
									flex: 1;
									display: -webkit-box;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
									overflow: hidden;
									word-wrap: break-word;
									word-break: break-all;
								}
							}

							.item_title_code {
								font-size: 24rpx;
								color: #777;
							}
						}
					}
				}
			}

			.void_goods {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 100%;
				height: 800rpx;
				// height: calc(100vh - 44px);
				// background-color: #fafafa;

				.img {
					width: 60%;
					height: 250rpx;
				}

				.tip {
					color: #aaa;
					font-size: 28rpx;
				}
			}

			.activeBtn {
				position: fixed;
				bottom: 0;
				right: 0;
				left: 0;
				z-index: 101;
				background-color: #fff;
				padding: 30rpx 0 40rpx;
				border-top: 1rpx solid #eee;

				.btn_Confirm {
					background-color: #f5f7fa;
					display: flex;
					justify-content: space-between;
					border-radius: 50rpx;
					margin: 0rpx 30rpx;

					.show_Confirm {
						font-size: 28rpx;
						display: flex;
						padding: 18rpx;

						.details {
							transform: scale(0.8);
							color: #007cff;
						}
					}

					.btn {
						background-color: #007cff;
						padding: 18rpx;
						border-radius: 0 50rpx 50rpx 0;

						.text {
							color: #fff;
							font-size: 28rpx;
						}
					}
				}
			}
		}

		.goodspopup {
			height: 1100rpx;
			background-color: #f6f7fa;
			position: relative;
			z-index: 2;

			.text_top_popup {
				text-align: center;
				font-size: 32rpx;
				padding: 24rpx 0;
				font-weight: bold;
				background-color: #fff;

				.icon {
					position: absolute;
					right: 30rpx;
					font-weight: 500;
				}
			}

			.goods_list {
				margin: 0 24rpx;
				width: calc(100% - 48rpx);
				overflow-y: auto;
				height: 90vh;

				&::-webkit-scrollbar {
					width: 0;
					height: 0;
					background-color: transparent;
				}

				.goods_item_bo {
					background-color: #fff;
					margin: 24rpx 0;
					border-radius: 10rpx;
					padding: 24rpx;

					.over_Insured {
						text-align: right;
						font-size: 24rpx;
						position: relative;
						top: 0;
						right: -12rpx;

						.tag {
							background-color: #007cff;
							color: #fff;
							font-size: 22rpx;
							padding: 2rpx 4rpx;
							border-radius: 4rpx;
							margin-right: 6rpx;

							&.tag_p {
								background-color: #ccc;
								color: #eee;
							}
						}
					}

					&:last-child {
						margin-bottom: 40vh;
					}

					.goods_item_info {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.item_title_status1 {
							position: absolute;
							top: 24rpx;
							right: 24rpx;
							font-size: 24rpx;
							background-color: #007cff;
							color: #fff;
							padding: 4rpx;
						}

						.item_title_status1 {
							position: absolute;
							top: 24rpx;
							right: 24rpx;
							font-size: 24rpx;
							background-color: #999;
							color: #fff;
							padding: 4rpx;
						}

						.goods_item_left {
							display: flex;
							// align-items: center;

							.item_img {
								width: 140rpx;
								height: 140rpx;
								background-color: #fff;
								border-radius: 4rpx;
								border: 1px solid #eee;
							}

							._img {
								width: 140rpx;
								height: 140rpx;
								border-radius: 4rpx;
							}

							.item_title {
								font-size: 24rpx;
								margin-left: 20rpx;
								flex: 1;
								display: flex;
								flex-direction: column;
								row-gap: 10rpx;

								.item_title_name {
									font-weight: bold;
									font-size: 28rpx;
									display: -webkit-box;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
									overflow: hidden;
									word-wrap: break-word;
									word-break: break-all;
								}

								.item_title_brand {
									font-size: 28rpx;
									display: -webkit-box;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
									overflow: hidden;
									word-wrap: break-word;
									word-break: break-all;
								}

								.item_title_text {
									font-size: 24rpx;
									color: #777;
									display: flex;
									flex-wrap: wrap;

									.item_title_color {
										color: #000;
										flex: 1;
										display: -webkit-box;
										-webkit-line-clamp: 2;
										-webkit-box-orient: vertical;
										overflow: hidden;
										word-wrap: break-word;
										word-break: break-all;
									}
								}

								.item_title_code {
									font-size: 24rpx;
									color: #999;
									display: flex;
									flex-wrap: wrap;

									.item_title_color {
										color: #000;
										flex: 1;
										display: -webkit-box;
										-webkit-line-clamp: 2;
										-webkit-box-orient: vertical;
										overflow: hidden;
										word-wrap: break-word;
										word-break: break-all;
									}
								}
							}
						}
					}
				}

				.goods_item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					background-color: #fff;
					margin: 24rpx 0;
					border-radius: 10rpx;
					padding: 24rpx;

					&:last-child {
						margin-bottom: 40vh;
					}

					position: relative;

					.item_title_status1 {
						position: absolute;
						top: 24rpx;
						right: 24rpx;
						font-size: 24rpx;
						background-color: #007cff;
						color: #fff;
						padding: 4rpx;
					}

					.item_title_status1 {
						position: absolute;
						top: 24rpx;
						right: 24rpx;
						font-size: 24rpx;
						background-color: #999;
						color: #fff;
						padding: 4rpx;
					}

					.goods_item_left {
						display: flex;
						// align-items: center;

						.item_img {
							width: 140rpx;
							height: 140rpx;
							background-color: #fff;
							border-radius: 4rpx;
							border: 1px solid #eee;
						}

						._img {
							width: 140rpx;
							height: 140rpx;
							border-radius: 4rpx;
						}

						.item_title {
							font-size: 24rpx;
							margin-left: 24rpx;
							flex: 1;

							.item_title_name {
								margin-bottom: 10rpx;
								font-weight: bold;
								font-size: 28rpx;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
								overflow: hidden;
								word-wrap: break-word;
								word-break: break-all;
							}

							.item_title_brand {
								font-size: 28rpx;
								margin-bottom: 10rpx;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
								overflow: hidden;
								word-wrap: break-word;
								word-break: break-all;

								.dot {
									font-weight: bold;
									padding: 0 10rpx;
								}
							}

							.item_title_text {
								font-size: 24rpx;
								color: #777;
								margin-bottom: 10rpx;
								display: flex;
								flex-wrap: wrap;

								.item_title_color {
									color: #000;
									flex: 1;
									display: -webkit-box;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
									overflow: hidden;
									word-wrap: break-word;
									word-break: break-all;
								}
							}

							.item_title_code {
								font-size: 24rpx;
								color: #777;
								display: flex;
								flex-wrap: wrap;

								.item_title_color {
									color: #000;
									flex: 1;
									display: -webkit-box;
									-webkit-line-clamp: 2;
									-webkit-box-orient: vertical;
									overflow: hidden;
									word-wrap: break-word;
									word-break: break-all;
								}
							}
						}
					}
				}
			}

			.void_goods {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 100%;
				height: 60vh;

				.img {
					width: 60%;
					height: 250rpx;
				}

				.tip {
					color: #aaa;
					font-size: 28rpx;
				}
			}
		}
	}
</style>
